/*
 * @Date: 2020-06-08 10:03:10
 * @LastEditors: fy
 * @LastEditTime: 2020-09-22 15:09:51
 * @FilePath: /nearby-common/src/monitor/lib/blankScreen.js
 */
// 空白屏
import Tracker from "../utils/tracker";
import onload from '../utils/onload';
import elementsFromPointPolyfill from 'polyfill-elements-from-point'

elementsFromPointPolyfill(window)

export function blankScreen(config) {
  // 哪些算是空白节点
  let wrapperElements = ["html", "body", "#content", ".box"];
  // 空节点计数
  let emptyPoints = 0;



  function isWrapper(el) {
    let selector = getSelector(el);
    if (wrapperElements.includes(selector)) {
      emptyPoints++;
    }
  }

  function getSelector(el) {

    if (el.id) {
      return "#" + el.id;
    } else if (el.className) {
      return "." + el.className.split(' ').filter(item => !!item).join('.');
    } else {
      return el.nodeName.toLowerCase();
    }
  }

  onload(function () {
    // if (typeof document !== 'undefined' && typeof document.elementsFromPoint === 'undefined') {
    //   document.elementsFromPoint = elementsFromPointPolyfill;
    // }
    for (let i = 1; i <= 9; i++) {
      let xElements = document.elementsFromPoint(
        (window.innerWidth * i) / 100,
        window.innerHeight / 2
      );
      let yElements = document.elementsFromPoint(
        window.innerWidth / 2,
        (window.innerHeight * i) / 100
      );
      isWrapper(xElements[0])
      isWrapper(yElements[0])
    }
    if (emptyPoints > 16) {
      new Tracker(config).sendError({
        kind: 'stability',
        type: 'blank screen',
        emptyPoints,
        screen: window.screen.width + 'x' + window.screen.height,
        viewPoint: window.innerWidth + 'x' + window.innerHeight,
      })
    }
  })

  // function elementsFromPointPolyfill(x, y) {
  //   var elements = [];
  //   var pointerEvents = [];
  //   var el;

  //   do {
  //     if (el !== document.elementFromPoint(x, y)) {
  //       el = document.elementFromPoint(x, y);
  //       elements.push(el);
  //       pointerEvents.push(el.style.pointerEvents);
  //       el.style.pointerEvents = 'none';
  //     } else {
  //       el = null;
  //     }
  //   } while (el);

  //   for (var i = 0; i < elements.length; i++) {
  //     elements[i].style.pointerEvents = pointerEvents[i];
  //   }

  //   return elements;
  // }
}


